from django.contrib import admin

from .models import (
    Campus,
    College,
    Department,
    Association,
    DepartmentPost,
    AssociationPost,
)
from .admin_forms import (
    CampusAdminForm,
    CollegeAdminForm,
    DepartmentAdminForm,
    AssociationAdminForm,
    DepartmentPostAdminForm,
    AssociationPostAdminForm,
)


# Register your models here.
@admin.register(Campus)
class CampusAdmin(admin.ModelAdmin):
    form = CampusAdminForm

    list_display = ('name', 'status', 'created_time')
    search_fields = ['name', ]
    date_hierarchy = 'created_time'

    fieldsets = (
        ('基本内容', {
            'description': '校区的基本内容',
            'fields': (
                'name',
                'status',
                'created_time',
                'desc',
            ),
        }),
    )


@admin.register(College)
class CollegeAdmin(admin.ModelAdmin):
    form = CollegeAdminForm

    list_display = ('name', 'status', 'created_time')
    search_fields = ['name', ]
    date_hierarchy = 'created_time'

    fieldsets = (
        ('基本内容', {
            'description': '院系的基本内容',
            'fields': (
                'name',
                'campus',
                'status',
                'created_time',
                'desc',
            ),
        }),
    )


@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    form = DepartmentAdminForm

    list_display = ('name', 'status', 'number', 'created_time')
    search_fields = ['name', ]
    date_hierarchy = 'created_time'
    # 部门管理员自动生成
    exclude = ('owner',)
    fieldsets = (
        ('基本内容', {
            'description': '部门的基本内容',
            'fields': (
                'name',
                'status',
                'image',
                'college',
                'created_time',
                'desc',
                'content',
            ),
        }),
        ('可选内容', {
            'fields': (
                'contact',
                'email',
                'principal',
            ),
        }),
        ('额外内容', {
            'classes': ('collapse',),
            'fields': (
                'number',
            )
        }),
    )

    def save_model(self, request, obj, form, change):
        """自动保存当前登录的用户为管理员"""
        obj.owner = request.user
        return super(DepartmentAdmin, self).save_model(request, obj, form, change)


@admin.register(Association)
class AssociationAdmin(admin.ModelAdmin):
    form = AssociationAdminForm

    list_display = ('name', 'status', 'number', 'created_time')
    search_fields = ['name', ]
    date_hierarchy = 'created_time'

    exclude = ('owner',)

    fieldsets = (
        ('基本内容', {
            'description': '社团的基本内容',
            'fields': (
                'name',
                'status',
                'image',
                'campus',
                'created_time',
                'desc',
                'content',
            ),
        }),
        ('可选内容', {
            'fields': (
                'contact',
                'email',
                'principal',
            ),
        }),
        ('额外内容', {
            'classes': ('collapse',),
            'fields': (
                'number',
            )
        }),
    )

    def save_model(self, request, obj, form, change):
        obj.owner = request.user
        return super(AssociationAdmin, self).save_model(request, obj, form, change)


@admin.register(DepartmentPost)
class DepartmentPostAdmin(admin.ModelAdmin):
    form = DepartmentPostAdminForm

    list_display = ('title', 'department', 'status', 'created_time', 'modified_time',)
    list_display_links = []

    list_filter = ['department', ]
    search_fields = ['title', 'department__name', ]
    date_hierarchy = 'modified_time'

    actions_on_top = True

    # 编辑页面
    save_on_top = True

    # 文章作者自动生成
    exclude = ('author',)

    fieldsets = (
        ('基本内容', {
            'description': '院系部门文章的基本内容',
            'fields': (
                'department',
                'title',
                'status',
            ),
        }),
        ('可选内容', {
            'fields': (
                'desc',
            ),
        }),
        ('主体内容', {
            'fields': (
                'content',
            ),
        }),
    )

    def save_model(self, request, obj, form, change):
        """自动保存当前登录的用户为作者"""
        obj.author = request.user
        return super(DepartmentPostAdmin, self).save_model(request, obj, form, change)


@admin.register(AssociationPost)
class AssociationPostAdmin(admin.ModelAdmin):
    form = AssociationPostAdminForm

    list_display = ('title', 'association', 'status', 'created_time', 'modified_time',)
    list_display_links = []

    list_filter = ['association', ]
    search_fields = ['title', 'association__name', ]
    date_hierarchy = 'modified_time'

    actions_on_top = True
    save_on_top = True

    exclude = ('author',)

    fieldsets = (
        ('基本内容', {
            'description': '校区社团文章的基本内容',
            'fields': (
                'association',
                'title',
                'status',
            ),
        }),
        ('可选内容', {
            'fields': (
                'desc',
            ),
        }),
        ('主体内容', {
            'fields': (
                'content',
            ),
        }),
    )

    def save_model(self, request, obj, form, change):
        obj.author = request.user
        return super(AssociationPostAdmin, self).save_model(request, obj, form, change)
